package com.md.common.utils.image;

import net.coobird.thumbnailator.Thumbnails;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 功能描述：图片处理工具<br>
 *     使用谷歌开源的Thumbnails图片处理项目对图片进行处理
 *
 * @author lujianping@oak168.com
 * @version 1.0
 * @date 2018-07-04 14:22
 * @since JDK 1.8
 */
public class ImageUtil {

    /**
     * 生成长宽的图片
     * @param originalFile
     * @param targetPicFile
     * @param width
     * @param height
     * @throws IOException
     */
    public static void resize(File originalFile, File targetPicFile, String targetPicFormat, int width, int height) throws IOException {
        Thumbnails.of(originalFile).size(width, height).outputFormat(targetPicFormat).toFile(targetPicFile);
    }

    /**
     * 生成长宽的图片
     * @param fileIs
     * @param targetPicFile
     * @param width
     * @param height
     * @throws IOException
     */
    public static void resize(InputStream fileIs, File targetPicFile, String targetPicFormat, int width, int height) throws IOException {
        Thumbnails.of(fileIs).size(width, height).outputFormat(targetPicFormat).toFile(targetPicFile);
    }

    /**
     * 生成长宽的图片
     * @param originalFile
     * @param targetFilePath
     * @param width
     * @param height
     * @throws IOException
     */
    public static void resize2(File originalFile, String targetFilePath, int width, int height) throws IOException {
        Thumbnails.of(originalFile).size(width, height).toFile(targetFilePath);
    }

    public static  void main(String[] arg) throws IOException, InterruptedException {
//        resize2(new File("D:\\1.jpg"), "D:\\2.jpg", 100, 100);
        File f = new File("D:\\2.png");
        resize(new File("D:\\1.jpg"), f, "png", 500, 500);
    }
}
